/**
 * 
 */
package info.niwota.home;

import info.niwota.home.fm.Dialogs;
import info.niwota.home.fm.Dialogs.ItemInfo;
import info.niwota.home.fm.Dialogs.ItemListAdapter;
import instant.openvfs.home.Debug;
import instant.openvfs.home.Preferences;

import java.util.List;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.TabActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageButton;
import android.widget.TabHost;

/**
 * @author qiangli
 * 
 */
public class HomeActivity extends TabActivity implements OnClickListener,
		OnLongClickListener {

	private static final String TAG = "HomeActivity";

	private static final int MAX_TAB = 2;

	//private ImageButton mQuickViButton;

	private ImageButton mShowButton;
	
	//private ImageButton mStartButton;

	private ItemListAdapter adapter;

	private static final int[] buttonImages = new int[] {
			R.drawable.view_thumbnail32, R.drawable.view_list32,
			R.drawable.view_fling32 };

	private static final int DIALOG_SET_TABS = 1;

	public void onCreate(Bundle savedInstanceState) {
		if (Debug.DEBUG) {
			Log.d(TAG, "savedInstanceState: " + savedInstanceState);
		}
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home_tabhost);

		final Resources res = getResources();
		final TabHost tabHost = getTabHost();
		TabHost.TabSpec spec;
		Intent intent;

		//
		intent = new Intent().setClass(this, HomeTabThumbnail.class);
		spec = tabHost.newTabSpec("thumbnail").setIndicator("Thumbnail",
				res.getDrawable(R.drawable.view_thumbnail32))
				.setContent(intent);
		tabHost.addTab(spec);

		//
		intent = new Intent().setClass(this, HomeTabList.class);
		spec = tabHost.newTabSpec("list").setIndicator("List",
				res.getDrawable(R.drawable.view_list32)).setContent(intent);
		tabHost.addTab(spec);
	
		//
		intent = new Intent().setClass(this, HomeTabFling.class);
		spec = tabHost.newTabSpec("fling").setIndicator("Fling",
				res.getDrawable(R.drawable.view_list32)).setContent(intent);
		tabHost.addTab(spec);
		
		//
		int tab = Preferences.getFlingMode(this);
		//
		initTabControl(savedInstanceState);
		//
		setCurrentTab(tab);
	}

	private void initTabControl(Bundle savedInstanceState) {
		//mStartButton = (ImageButton) findViewById(R.id.start);
		mShowButton = (ImageButton) findViewById(R.id.show_tab);
		//mQuickViButton = (ImageButton) findViewById(R.id.quickbar_vi);
		//
		//mStartButton.setOnClickListener(this);
		mShowButton.setOnClickListener(this);
		//mQuickViButton.setOnClickListener(this);
		//
		mShowButton.setOnLongClickListener(this);

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (Debug.DEBUG) {
			Log.d(TAG, "onKeyDown " + keyCode);
		}
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			finish();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	
	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);

		// Close the menu
		if (Intent.ACTION_MAIN.equals(intent.getAction())) {
			getWindow().closeAllPanels();
		}
	}

	public void onClick(View v) {
		final int id = v.getId();
		switch (id) {
//		case R.id.start:
//			AppHelper.startStartMenuActivity(this);
//			break;
//		case R.id.quickbar_vi:
//			AppHelper.startViActivity(this);
//			break;
		case R.id.show_tab: {
			// toggle
			final TabHost th = getTabHost();
			int ctab = th.getCurrentTab() + 1;
			setCurrentTab(ctab);
			break;
		}
		default:
		}
	}

	private void setCurrentTab(int ctab) {
		if (ctab > MAX_TAB) {
			ctab = 0;
		} else if (ctab < 0) {
			ctab = 0;
		}
		mShowButton.setImageResource(buttonImages[ctab]);
		final TabHost th = getTabHost();
		th.setCurrentTab(ctab);
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DIALOG_SET_TABS:
			return createTabsDialog();
		default:
		}
		return null;
	}

	@Override
	protected void onPrepareDialog(int id, Dialog dialog) {
		switch (id) {
		case DIALOG_SET_TABS:
			final Window w = dialog.getWindow();
			w.setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
			// Issue 3674
			w.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
					WindowManager.LayoutParams.FLAG_FULLSCREEN);
			if (adapter != null) {
				adapter.reload();
			}
			break;
		default:
		}
	}

	private Dialog createTabsDialog() {
		final Context ctx = this;
		final String[] items = ctx.getResources().getStringArray(
				R.array.tab_views);
		adapter = new ItemListAdapter(this) {
			public void loadItems(final Context context, List<ItemInfo> list) {
				final int ctab = getTabHost().getCurrentTab();
				for (int i = 0; i < items.length; i++) {
					ItemInfo item = new ItemInfo();
					item.setTitle(items[i]);
					boolean sel = (i == ctab);
					if (sel) {
						item.setIconRes(R.drawable.btn_check_buttonless_on);
					} else {
						item.setIconRes(0);
					}
					item.setIcon(null);
					//
					list.add(item);
				}
			}

			public void onClick(DialogInterface dialog, int which) {
				if (Debug.DEBUG) {
					Log.d(TAG, "onClick which:  " + which);
				}
				setCurrentTab(which);
			}
		};
		AlertDialog d = Dialogs.create(ctx, null, adapter);
		return d;
	}

	public boolean onLongClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.show_tab:
			showDialog(DIALOG_SET_TABS);
			break;
		}
		return false;
	}
}
